/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.loader.serialize.serializer.read.SerializerReadBytes;
import e.odbo.data.dao.BaseDAO;
import cn.ekuma.epos.businesslogic.RoleUtil;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.bean.Role;
import com.openbravo.data.model.Field;

/**
 *
 * @author Administrator
 */
public class RoleDAO extends BaseDAO<Role>{

    public RoleDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
            "ROLES",
            new Field[]{
                          new Field("ID",Datas.STRING,Formats.STRING),
                          new Field("NAME",Datas.STRING,Formats.STRING),
                          new Field("PERMISSIONS",Datas.BYTES,Formats.BYTEA),
                          new Field("ADVCODE",Datas.STRING,Formats.STRING),
                          new Field("ORDERTYPEPERMISSIONS",Datas.BYTES,Formats.BYTEA),
                          new Field("STATEPROCESSPERMISSIONS",Datas.BYTES,Formats.BYTEA)
                          },
            new int[] {0}
        );
    }

    public Role readValues(DataRead dr,Role obj) throws BasicException {
        if(obj==null)
        	obj=new Role();
        obj.setID(dr.getString(1));
        obj.setM_sName(dr.getString(2));
        obj.setPermissions(RoleUtil.formatPermissions(Formats.BYTEA.formatValue(dr.getBytes(3))));
        obj.setM_advCode(dr.getString(4));
        obj.setOrderTypePermissions(RoleUtil.formatOrderTypePermissions(Formats.BYTEA.formatValue(dr.getBytes(5))));
        obj.setStateProcessPermissions(RoleUtil.formatOrderTypePermissions(Formats.BYTEA.formatValue(dr.getBytes(6))));
        return obj;
    }

    @Override
    public void writeInsertValues(DataWrite dp, Role obj) throws BasicException {
         dp.setString(1, obj.getID());
         dp.setString(2, obj.getM_sName());
         dp.setBytes(3, (byte[]) Formats.BYTEA.parseValue(RoleUtil.buildXml(obj.getPermissions())));
         dp.setString(4, obj.getM_advCode());
         dp.setBytes(5, (byte[])Formats.BYTEA.parseValue(RoleUtil.buildOrderTypePermissionXml(obj.getOrderTypePermissions().values())));
         dp.setBytes(6, (byte[])Formats.BYTEA.parseValue(RoleUtil.buildOrderTypePermissionXml(obj.getStateProcessPermissions().values())));
    }

    public final String findRolePermissions(String sRole) {
        try {
            return Formats.BYTEA.formatValue(new PreparedSentence(s
                , "SELECT PERMISSIONS FROM ROLES WHERE ID = ?"
            , SerializerWriteString.INSTANCE
            , SerializerReadBytes.INSTANCE).find(sRole));
        } catch (BasicException e) {
            return null;
        }
    }

    public final String findRoleOrderTypePermissions(String sRole) {

        try {
            return Formats.BYTEA.formatValue( new PreparedSentence(s
                , "SELECT ORDERTYPEPERMISSIONS FROM ROLES WHERE ID = ?"
            , SerializerWriteString.INSTANCE
            , SerializerReadBytes.INSTANCE).find(sRole));
        } catch (BasicException e) {
            return null;
        }
    }

    public final String findRoleStateProcessPermissions(String sRole) {

        try {
            return Formats.BYTEA.formatValue( new PreparedSentence(s
                , "SELECT STATEPROCESSPERMISSIONS FROM ROLES WHERE ID = ?"
            , SerializerWriteString.INSTANCE
            , SerializerReadBytes.INSTANCE).find(sRole));
        } catch (BasicException e) {
            return null;
        }
    }
    

    public final String findRolePermissionsByAdvCode(String advCode) {
        try {
            return Formats.BYTEA.formatValue(new PreparedSentence(s
                , "SELECT PERMISSIONS FROM ROLES WHERE ADVCODE = ?"
            , SerializerWriteString.INSTANCE
            , SerializerReadBytes.INSTANCE).find(advCode));
        } catch (BasicException e) {
            return null;
        }
    }

    @Override
    public Class getSuportClass() {
       return Role.class;
    }

}
